package com.example.user.web;

import com.example.common.Ov.BankOv;
import com.example.common.Ov.LoginOv;
import com.example.common.Ov.UserInfoOv;
import com.example.common.Ov.UserOv;
import com.example.common.entity.Const;
import com.example.common.entity.Rsp;
import com.example.common.pojo.User;
import com.example.common.util.JWTUtil;
import com.example.user.feign.MessageFeign;
import com.example.user.service.BankServiceImpl;
import com.example.user.service.TransCodeServiceImpl;
import com.example.user.service.UserServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

@Api(value = "前台用户", description = " 前台用户管理接口")
@RestController
public class UserController {

    @Autowired
    UserServiceImpl userService;

    @Autowired
    BankServiceImpl bankService;

    @Autowired
    TransCodeServiceImpl transCodeService;

    /**
     * 校验用户名，密码，邮箱
     */
    @PostMapping("checkAccountPwdEmail")
    @ApiOperation("校验用户名，密码，邮箱")
    public Rsp checkAccountPwdEmail(@RequestBody User user) {
        return userService.checkAccountPwdEmail(user);
    }

    //获取验证码（用于登录时）
    @GetMapping("/sendMessageCode/{mobile}")
    @ApiOperation("获取验证码")
    public Rsp sendMessageCode(@PathVariable("mobile") String mobile) {
        return userService.getUserByMobile(mobile);
    }

    //手机号登录
    @PostMapping("/loginByMobile")
    @ApiOperation("手机号登录")
    public Map<String,Object> login(@RequestBody LoginOv loginOv){
        System.out.println(loginOv);
        //调用登录的方法
//        Rsp rsp = messageCodeService.login(loginOv);
        return userService.login(loginOv);
    }

    //账号登录方式
    @PostMapping("/login")
    @ApiOperation("账号登录方式")
    public Map<String,Object> login(@RequestBody User user){

        return userService.login(user);
    }



    //根据id获取用户信息
    @GetMapping("getUserById")
    @ApiOperation("根据id获取用户信息")
    public Rsp getUserById(Integer id){
        return userService.getUserById(id);
    }

    //获取用户信息
    @GetMapping("getUserInfo")
    @ApiOperation("获取用户信息")
    public Rsp getUserInfo(@RequestHeader("Authorization") String token){
        UserOv userOv = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        System.out.println(userOv);
        if (StringUtils.isEmpty(userOv)){
            return new Rsp(1,"登录已失效，请重新登录");
        }
        return userService.getUserById(userOv.getId());
    }

    //用户注册
    @PostMapping("register")
    @ApiOperation("用户注册")
    public Rsp register(@RequestBody User user){
        return userService.register(user);
    }

    //修改个人信息
    @PostMapping("updateMyself")
    @ApiOperation("修改个人信息")
    public Rsp updateMyself(@RequestHeader("Authorization") String token,@RequestBody User user){
        UserOv userOv = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        System.out.println(user);
        return userService.updateMyself(user);
    }

    //获取验证码（用于验证码银行卡时）
    @GetMapping("/getCode/{mobile}")
    @ApiOperation("获取验证码")
    public Rsp getCode(@PathVariable("mobile") String mobile) {
        return userService.getCode(mobile);
    }

    //注册存管
    @PostMapping("registerBank")
    @ApiOperation("修改个人信息")
    public Rsp registerBank(@RequestHeader("Authorization") String token,@RequestBody BankOv bankOv){
        UserOv userOv = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        System.out.println(bankOv);
        return bankService.registerBank(bankOv,userOv);
    }

    //充值
    @PostMapping("recharge")
    @ApiOperation("充值")
    public Rsp recharge(@RequestHeader("Authorization") String token,@RequestBody User user){
        UserOv userOv = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        return userService.recharge(userOv,user);
    }

    //提现
    @PostMapping("withdrawal")
    @ApiOperation("提现")
    public Rsp withdrawal(@RequestHeader("Authorization") String token,@RequestBody User user){
        UserOv userOv = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        return userService.withdrawal(userOv,user);
    }

    //获取账单列表
    @GetMapping("getTransCode")
    @ApiOperation("获取账单列表")
    public Rsp getTransCode(@RequestHeader("Authorization") String token){
        UserOv userOv = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        return transCodeService.getTransCode(userOv);
    }


    @PostMapping("getUserInfoById")
    public Rsp getUserInfoById(@RequestBody User user){
        return userService.getUserById(user.getId());
    }

    //付款减去账户余额
    @RequestMapping("subBalance")
    public int subBalance(@RequestBody User user, @RequestParam BigDecimal cost) {
        return userService.subBalance(user, cost);
    }
    //满标加账户余额
    @RequestMapping("addBalance")
    public int addBalance(@RequestParam Integer id, @RequestParam BigDecimal cost) {
        return userService.addBalance(id, cost);
    }
    //流标退款
    @RequestMapping("refund")
    public int refund(@RequestParam Integer borrowId) {
        return userService.refund(borrowId);
    }
}
